package com.pirestupppai.ui.activity;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.pirestupppai.R;
import com.pirestupppai.adapter.FragmentAdapter;
import com.pirestupppai.adapter.PsListAdapter;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.fragment.PsListFragment;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.rxbus.RxBus.RXBUS_FINISH_ACTIVITY;

/**
 * 帮P列表
 *
 * @author: xiaoluo
 * @date: 2017-04-21 16:49
 */
public class PsListActivity extends BaseActivity {

    @BindView(R.id.ps_tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.ps_viewpager)
    ViewPager viewPager;
    @BindView(R.id.back_btn)
    ImageView backBtn;

    private FragmentAdapter mFragmentAdapter;

    private int mType;
    private Subscription mSubscription;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_ps_list;
    }

    @Override
    protected void initViews() {
        mType = getIntent().getIntExtra(Constant.IntentKey.PS_TYPE, PsListAdapter.PS_TYPE_NORMAL);
        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        if (mType == PsListAdapter.PS_TYPE_NORMAL) {
            mFragmentAdapter.addFragment(new PsListFragment(true, PsListAdapter.PS_TYPE_INPROGRESS, "pending"), "进行中");
            mFragmentAdapter.addFragment(new PsListFragment(true, PsListAdapter.PS_TYPE_REPLY_NOW, "finished"), "已完成");
        } else {
            mFragmentAdapter.addFragment(new PsListFragment(false, PsListAdapter.PS_TYPE_INPROGRESS, "pending"), "进行中");
            mFragmentAdapter.addFragment(new PsListFragment(false, PsListAdapter.PS_TYPE_REPLY_NOW, "finished"), "已完成");
        }
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(1);
        // 将TabLayout与viewpager绑定
        tabLayout.setupWithViewPager(viewPager);

//        tabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                TabLayoutHelper.setIndicatorWidth(tabLayout,60,60);
//            }
//        });
    }

    @Override
    protected void initListeners() {
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initLogic() {
        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_FINISH_ACTIVITY, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        finish();
                    }
                });
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

}
